package com.onenine.app.activity;

import android.app.Fragment;
import android.content.Intent;
import android.graphics.Color;
import android.media.MediaPlayer;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.view.View;
import android.widget.Button;

import com.onenine.app.R;
import com.onenine.app.application.NineteenApp;
import com.onenine.app.event.PermissionsEvent;
import com.onenine.app.fragment.LoginInputInfoFragment;
import com.onenine.app.fragment.LoginMainFragment;
import com.onenine.app.listener.OnLoginListener;
import com.onenine.app.utils.CHelper;
import com.onenine.app.utils.DataType;
import com.onenine.app.view.LocalVideoView;
import com.onenine.app.view.uppanel.SlidingUpPanelLayout;
import com.onenine.base.base.BaseActivity;
import com.onenine.base.utils.StatusBarUtils;
import com.onenine.common.util.FragmentUtils;
import com.onenine.common.util.KeyboardUtils;
import com.umeng.share.UMLogin;

import org.greenrobot.eventbus.EventBus;

/**
 * 第二版登录界面
 */

public class LoginActivity extends BaseActivity implements SlidingUpPanelLayout
        .PanelSlideListener, OnLoginListener, View.OnClickListener, MediaPlayer.OnPreparedListener, MediaPlayer.OnCompletionListener {

    private SlidingUpPanelLayout mUpPanelLayout;
    private View mvBackground;
    private LocalVideoView mVideoView;
    private Button mBtnStart;

    @Override
    protected int getLayout() {
        return R.layout.activity_login;
    }

    @Override
    protected void initViews() {
        super.initViews();
        BaseActivity.sLoadingColor = ActivityCompat
                .getColor(this, R.color.theme_color_male);
        mBtnStart = findViewById(R.id.btn_start);
        mVideoView = findViewById(R.id.vv_view);
        mvBackground = findViewById(R.id.decor_view);
        mUpPanelLayout = findViewById(R.id.sliding_layout);
        mUpPanelLayout.setIsClickToggle(false);
        mToolbar.setLayoutBackgroundColor(Color.TRANSPARENT);
        mToolbar.setBottomDividerVisible(false);
        mToolbar.setLeftDrawable(R.drawable.ic_back_white);
        FragmentUtils.replace(getFragmentManager()
                , new LoginMainFragment(), R.id.fl_content, false);
        mVideoView.startVideo(CHelper.getRawResouceString(R.raw.login_small_video), true);
        StatusBarUtils.setTransparentForImageView(this, null);
    }

    @Override
    protected void onResume() {
        super.onResume();
        mVideoView.resumeVideo();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mVideoView.pauseVideo();
    }

    @Override
    protected void initListeners() {
        super.initListeners();
        mUpPanelLayout.addPanelSlideListener(this);
        mToolbar.setOnLeftClickListener(this);
        mBtnStart.setOnClickListener(this);
    }

    @Override
    protected void setStatusBarStyle() {
    }

    @Override
    public void showLoading() {
        super.showLoading();
    }

    @Override
    public void showLoading(String msg) {
        super.showLoading(msg);
    }

    @Override
    public void replaceLoading(String msg) {
        super.replaceLoading(msg);
    }

    @Override
    public void dismissLoading() {
        super.dismissLoading();
    }

    @Override
    public void updateBackground(int sex) {
        if (sex == DataType.SEX_MALE) {
            mVideoView.pauseVideo();
            mvBackground.setBackgroundResource(R.drawable.shape_btn_gradient_bg_male);
        } else if (sex == DataType.SEX_FEMALE) {
            mVideoView.pauseVideo();
            mvBackground.setBackgroundResource(R.drawable.shape_btn_gradient_bg_female);
        } else {
            mVideoView.resumeVideo();
            mvBackground.setBackgroundResource(0);
        }
    }

    @Override
    public void finishActivity() {
        finish();
    }

    @Override
    public SlidingUpPanelLayout getUpPanelLayout() {
        return mUpPanelLayout;
    }

    @Override
    public void onPanelSlide(View panel, float slideOffset) {

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMLogin.onActivityResult(this, requestCode, resultCode, data);
        if (requestCode == LoginInputInfoFragment.RC_AVATAR) {
            //交由头像页面处理
            Fragment fragment = FragmentUtils.findFragment(getFragmentManager(), LoginInputInfoFragment.class);
            if (fragment != null) {
                fragment.onActivityResult(requestCode, resultCode, data);
            }
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == LoginInputInfoFragment.RC_AVATAR) {
            //交由头像页面处理
            Fragment fragment = FragmentUtils.findFragment(getFragmentManager(), LoginInputInfoFragment.class);
            if (fragment != null) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    fragment.onRequestPermissionsResult(requestCode, permissions, grantResults);
                } else {
                    EventBus.getDefault().post(new PermissionsEvent(requestCode, permissions, grantResults));
                }
            }
        }
    }

    @Override
    public void onPanelStateChanged(View panel, SlidingUpPanelLayout.PanelState previousState, SlidingUpPanelLayout.PanelState newState) {
        if (newState == SlidingUpPanelLayout.PanelState.COLLAPSED) {
            if (previousState == SlidingUpPanelLayout.PanelState.DRAGGING) {
                if (getFragmentManager().popBackStackImmediate()) {
                    mUpPanelLayout.setPanelState(SlidingUpPanelLayout.PanelState.ANCHORED);
                } else if (!isStartButtonShown()) {
                    goBack();
                }
            }
        } else if (newState == SlidingUpPanelLayout.PanelState.ANCHORED) {
            if (KeyboardUtils.isSoftInputVisible(LoginActivity.this)) {
                KeyboardUtils.hideSoftInput(LoginActivity.this);
            }
        }
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.tv_left_title) {
            onBackPressed();
        } else if (v.getId() == R.id.btn_start) {
            mBtnStart.setVisibility(View.GONE);
            mUpPanelLayout.setPanelState(SlidingUpPanelLayout.PanelState.ANCHORED);
        }
    }

    @Override
    public void onCompletion(MediaPlayer mp) {
        mp.start();
        mp.setLooping(true);
    }

    @Override
    public void onPrepared(MediaPlayer mp) {
        mp.setVideoScalingMode(MediaPlayer.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING);
        mp.start();
    }

    @Override
    public void onBackPressed() {
        if (getFragmentManager().getBackStackEntryCount() > 0) {
            if (mUpPanelLayout.getPanelState() == SlidingUpPanelLayout.PanelState.EXPANDED
                    && mUpPanelLayout.isTouchEnabled()) {
                mUpPanelLayout.setPanelState(SlidingUpPanelLayout.PanelState.ANCHORED);
            } else {
                if (getFragmentManager().popBackStackImmediate()) {
                    if (mUpPanelLayout.getPanelState() == SlidingUpPanelLayout.PanelState.COLLAPSED
                            || mUpPanelLayout.getPanelState() == SlidingUpPanelLayout.PanelState.ANCHORED) {
                        mUpPanelLayout.setPanelState(SlidingUpPanelLayout.PanelState.ANCHORED);
                    }
                }
            }
        } else {
            goBack();
        }
    }

    private void goBack() {
        if (!isStartButtonShown()) {
            mUpPanelLayout.setPanelState(SlidingUpPanelLayout.PanelState.COLLAPSED);
            NineteenApp.sHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    mBtnStart.setVisibility(View.VISIBLE);
                }
            }, 200);
        } else {
            finishActivity();
        }
    }

    private boolean isStartButtonShown() {
        return mBtnStart.getVisibility() == View.VISIBLE;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mVideoView.destroyVideo();
    }

}
